//
// Created by yangchao on 2022/4/19.
// 3. 无重复字符的最长子串: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
//

#include <string>
#include <unordered_set>
using namespace std;

class LongestSubstring {
public:
    int lengthOfLongestSubstring(string s) {
        if (s.size() == 0) return 0;
        int len = s.size();
        unordered_set<char> set;
        int res = 1;
        int slow = 0, fast = 0;
        while (fast < len) {
            //表明set集合中没有重复的字符
            while (fast < len && set.find(s[fast]) == set.end()) {
                set.insert(s[fast]);
                fast++;
            }
            set.erase(s[slow]);
            res = max(res,fast-slow);
            slow++;
        }
        return res;
    }
};


